<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px; margin-bottom: 10px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>酒店管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加酒店</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="绑定部门" prop="region">
        <el-select v-model="ruleForm.region" placeholder="请选择部门">
          <el-option label="部门一" value="shanghai"></el-option>
          <el-option label="部门二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="酒店名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="酒店地址" prop="address">
        <el-input v-model="ruleForm.address"></el-input>
      </el-form-item>
      <el-form-item label="停车场" prop="parkingLot">
        <el-input v-model="ruleForm.parkingLot"></el-input>
      </el-form-item>
      <el-form-item label="标签(多个用逗号隔开)" prop="badge">
        <el-input v-model="ruleForm.badge"></el-input>
      </el-form-item>

      <el-form-item label="隔离酒店(默认不是)" prop="delivery">
        <el-switch v-model="ruleForm.delivery"></el-switch>
      </el-form-item>
      <!--   上传酒店图片   -->
      <el-form-item label="封面" prop="cover">
        <el-upload
            action="https://jsonplaceholder.typicode.com/posts/"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="resetForm.cover" alt="">
        </el-dialog>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        name: '',
        address: '',
        parkingLot: '',
        badge: '',
        region: '',
        cover: '',
        delivery: false,
      },
      rules: {
        name: [
          {required: true, message: '请输入酒店名称', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入酒店地址', trigger: 'blur'},
          {min: 3, max: 35, message: '长度在 5 到 35 个字符', trigger: 'blur'}
        ],
        parkingLot: [
          {required: true, message: '是否有停车场', trigger: 'blur'},
          {min: 3, max: 35, message: '长度在 5 到 35 个字符', trigger: 'blur'}
        ],
        badge: [
          {required: true, message: '请输入酒店标签', trigger: 'blur'},
          {min: 3, max: 35, message: '长度在 5 到 35 个字符', trigger: 'blur'}
        ],
        region: [
          {required: true, message: '请选择部门', trigger: 'change'}
        ],
      }
    };
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.cover = file.url;
      this.dialogVisible = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9000/api/hotel/add';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm, {arrayFormat: 'repeat'});
          console.log('formData = ' + formData);
          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log('responseBody = ');
            console.log(responseBody);
            if (responseBody.code == 200) {
              this.$message({
                message: '添加酒店成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.msg);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>

</style>